/*******************************************************************************
 * This file is part of blueprint4j www.blueprint4j.org.
 * 
 *     blueprint4j is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     blueprint4j is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 * 
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with blueprint4j. If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package blueprint4j.utils;

import java.io.*;
import java.text.*;
import java.util.*;

/**
 * This class is a logging class that writes it's log out to a file.
 * The home directory of all the files is given when the class is created.
 * Per hour a new file is created the nameing convention is as follows.
 * <CODE>
 * Log_YYYY_MM_DD_HH.log
 * </CODE>
 * This way the log file will be easy to sort by date in any file system as well
 * as small. For hours when no loggin occures there will be no log file.
 */
public class LoggingFileHTML{
	SimpleDateFormat date_format = new SimpleDateFormat("yyyy-MM-dd 'at' HH'h'mm.ss");
	static SimpleDateFormat filename_format = new SimpleDateFormat("'Log'yyyy-MM-dd_HH'.html'");
	String lastfilename = null;
	String location;
	String stop_date;
	OutputStreamWriter output = null;
	static String newname = null;
	boolean inexception = false;
	boolean dont_log = false;

	public LoggingFileHTML(String p_location){
		location = p_location;
		if (!p_location.endsWith("\\") && !p_location.endsWith("/")) {
			p_location = p_location + '/';
		}
	}

	public static File getLastLogFile() {
		if (newname != null) {
			return new File(newname);
		}
		return null;
	}

	public void write(String html) {
		try {
			buildOutputFile();
			output.write(html);
			output.write("\n");
			output.flush();
			output.close();
		} catch (IOException ioe) {
			ioe.printStackTrace();
		}
	}

	/**
	 * Build the new file writer for outputing the logs to.
	 */
	private void buildOutputFile()
	throws IOException {
		File directory = new File(location);
		if (!directory.exists()) {
			if (!directory.mkdirs()) {
				throw new IOException("Can't build [" + directory + "]");
			}
		}
		// Work out name that would be used now
		newname = location + filename_format.format(new Date());
		// If file does exist then use that file, else create new one
		try {
			output = new FileWriter(newname, true);
		} catch (FileNotFoundException fnfe) {
			output = new FileWriter(newname);
		}
	}

	public void close() {
		try {
			if (output != null) {
				output.close();
			}
		} catch (IOException ioe) {
			ioe.printStackTrace();
		}
	}
}

